<div class="row">
    <div class="col-md-12">
        <form id="roleAddForm">
            <input type="hidden" class="form-control" name="id" value="${bean.id}">
            <div class="modal-body">
                <div class="form-group" >
                    <label id="nameLabel">名称</label>
                    <input type="text" class="form-control" name="role" id="role" placeholder="输入英文字母,如kefu" value="${bean.role!}">
                </div>
                <div class="form-group">
                    <label>角色描述</label>
                    <input type="text" class="form-control" name="description" id="description" placeholder="输入角色简称" value="${bean.description!}">
                </div>
                <div class="form-group">
                    <label>权限</label>
                    <ul id="resztree" class="ztree"></ul>
                </div>
                <div class="form-group">
                    <label>状态</label>
                    <select name="status" class="form-control select2" style="width: 100%;">
                        <option value="1" <#if bean.status == '1'>selected</#if> >启用</option>
                        <option value="0" <#if bean.status == '0'>selected</#if> >停用</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <div class="pull-right">
                    <button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><i class="fa fa-close"></i>关闭</button>
                    <button type="button" class="btn btn-primary btn-sm" onclick="roleSave()"><i class="fa fa-save"></i>保存</button>
                </div>
            </div>
        </form>
    </div>
</div>

<script type="text/javascript">

    $(function () {
        /***ztree**/
        var resSetting = {
            check: {
                enable: true,
                chkboxType: { "Y" : "ps", "N" : "ps" }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },

            callback: {
            }
        };


        var resZNodes =[
		<#list list as o>
            { "id":${o.id},<#if o.type == 'directory'>"open":true,</#if> "pId":${o.parentId}, "name":"${o.name}" ${(o.id == 1)?string(",nocheck:true","")}  ${(",${bean.resourceIds!},"?contains(","+o.id+","))?string(",checked:true","")}},
		</#list>
        ];


        $('#lgModal').on('show.bs.modal', function (e) {
            //初始化树s
            $.fn.zTree.init($("#resztree"), resSetting, resZNodes);
        });

        $('#lgModal').on('hide.bs.modal', function (e) {
            //$.fn.zTree.destroy("resztree");
            //$.fn.zTree.init($("#resztree"), setting, zNodes);
            //$(this).removeData("bs.modal");
           // $(".modal-body").children().remove();
        });
    })

    var roleSave = function (){
        $("span").remove(".errorClass");
        $("br").remove(".errorClass");
        var status = 1;
        if($("#name").val()==""){
            $("#nameLabel").prepend('<span class="errorClass" style="color:red">*名称不能为空</span><br class="errorClass"/>');
            status = 0;
        }
        if(status == 0){
            return false;
        }else{
            //处理选中的权限
            var resourceIds = "";
            var zTree = $.fn.zTree.getZTreeObj("resztree");
            nodes = zTree.getCheckedNodes(true);
            for(var i in nodes){
                resourceIds = resourceIds + nodes[i].id + ",";
            }
            resourceIds = resourceIds.substr(0,resourceIds.length-1);

            $.ajax({
                url: '/admin/role/save',
                type: 'post',
                dataType: 'text',
                data: $("#roleAddForm").serialize() + "&resourceIds="+resourceIds,
                success: function (data) {
                    $("#lgModal").modal('hide');
                    alertMsg("添加成功","success");
                    roleReload();
                }
            });
        }
    }


</script>